 
import echarts from 'echarts';
// 引入柱状图
import 'echarts/lib/chart/bar';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';

function equipMgtCtrl($log, $http, $scope, $timeout, cfpLoadingBar) {
  'ngInject';

  let _this = this;

  this.monthEquipList = {}; // 存放机械设备情况汇总

  var myDate = new Date();  // 获取当前时间
  _this.years = myDate.getFullYear();

  this.yearList = [
    {
      id:'01',
      year:'2015'
    },
    {
      id:'02',
      year:'2016'
    }
  ]

  // 机械设备进出场情况汇总
  this.myChartMonthInit = () => {
    let option = {
      color: ['#59b0e7','#7cc78a','#a9a9a9'],
      title : {
        text: '机械设备进出场情况汇总',
        x:'center',
        textStyle: {color: '#666'}
      },
      tooltip : {
        trigger: 'axis'
      },
      legend: {
        data:['本月进场设备数','本月离场设备数','本月在线设备数'],
        left:20,
        top:30
      },
      calculable: true,
      xAxis : [
        {
          type : 'category',
          data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
        }
      ],
      yAxis : [
        {
          show:false,
          type : 'value',
        }
      ],
      series : [
        {
          name:'本月进场设备数',
          type:'bar',
          stack: '全部',
          barWidth : 30,
          itemStyle: {normal: {
            label : {show: true,
              position:'top'}
          }},
          data:_this.data4ChartShow.countInto
        },
        {
          name:'本月离场设备数',
          type:'bar',
          stack: '全部',
          barWidth : 30,
          itemStyle: {normal: {
            label : {
              show: true,
              position: 'bottom'
            }
          }},
          data:_this.data4ChartShow.countOut
        },
        {
          name:'本月在线设备数',
          type:'line',
          label: {
                    normal: {
                      show: true,
                      position: 'top'
                    }
                  },
          data:_this.data4ChartShow.countIn
        }
      ]
    };
    let myChart = echarts.init(document.getElementById('mechanical-equipment-total'));
    myChart.setOption(option);
  }

  // 维保情况echarts
  this.maintenanceEcharts = () => {
    let option = {
      color: ['#6dcbe4'],
      tooltip : {
        trigger: 'axis'
      },
      legend: {
        left:20,
        top:0,
        data:['维保次数']
      },
      toolbox: {
        show : true,
        feature : {
          dataView : {show: true, readOnly: false},
          magicType : {show: true, type: ['line', 'bar']},
          restore : {show: true},
          saveAsImage : {show: true}
        }
      },
      calculable : true,
      xAxis : [
        {
          type : 'category',
          data : _this.maintenanceMonth
        }
      ],
      yAxis : [
        {
          type : 'value',
        }
      ],
      series : [
        {
          name:'维保次数',
          type:'bar',
          data:_this.maintenanceList,
          barWidth : 30,
          markPoint : {
            data : [
              {type : 'max', name: '最大值'},
              {type : 'min', name: '最小值'}
            ]
          }
        }
      ]
    };
    let myChart = echarts.init(document.getElementById('maintenance-condition'));
    myChart.setOption(option);
  }

  // 检测情况echarts
  this.inspectionEcharts = () => {
    let option = {
      tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
      },
      color: ['#f6846d', '#fcc638'],
      legend: {
        top:40,
        right:20,
        data: ['检测通过','检测未通过']
      },
      title : {
        text: '检测合格情况',
        x:'center',
        top:160,
        textStyle: {color: '#666'}
      },
      toolbox: {
        show : true,
        feature : {
          dataView : {show: true, readOnly: false},
          magicType : {show: true, type: ['line', 'bar']},
          restore : {show: true},
          saveAsImage : {show: true}
        }
      },
      calculable : true,
      series : [
        {
          name:'检测',
          type: 'pie',
          radius: ['45%', '65%'],
          center: ['50%', '60%'],
          data:[
            {value:_this.inspectionList.checkPass, name:'检测通过'},
            {value:_this.inspectionList.checkNoPass, name:'检测未通过'},
          ],
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    let myChart = echarts.init(document.getElementById('detection-condition'));
    myChart.setOption(option);
  }

  // 得到年
  this.syearOne = (param) => {
    _this.years = param.year; 
    this.getEquipTotal();  
  }

  // 获取机械设备进出场情况汇总
  this.getEquipTotal = () => {
    $http.get(`${SERVER_CONTEXT}/api/deviceinfo/queryDeviceInOutRecordByAll/${_this.years}`
          )
        .then((resp) => {
          this.monthEquipList.countIn = [];
          this.monthEquipList.countOut = [];
          this.monthEquipList.countInto = [];
          resp.data.forEach((item) => {           
            this.monthEquipList.countIn.push(item.countIn); 
            this.monthEquipList.countOut.push(item.countOut);
            this.monthEquipList.countInto.push(item.countInto);           
          });

          this.data4ChartShow = {
            "countIn": this.monthEquipList.countIn,
            "countOut": this.monthEquipList.countOut,
            "countInto": this.monthEquipList.countInto
          }
          this.myChartMonthInit(); // echarts
        });
  }

  // 获取维保情况的维保次数
  this.getMaintenance = () => {
    $http.get(`${SERVER_CONTEXT}/api/devicemaintainrecord/countMaintainData/`
          )
        .then((resp) => {
          this.maintenanceQueryList = resp.data;
          this.maintenanceList = [];
          this.maintenanceMonth = [];
          this.maintenanceQueryList.forEach((item) => {
            for(key in item){
              this.maintenanceList.push(item[key]);
              this.maintenanceMonth.push(key);
            }
          })
          this.maintenanceEcharts(); // echarts
        });
  }

  this.paginationData = {
    pageNum: 1,
    pageSize: 6,
    total: '6',
  }

  // 获取维保情况的设备记录
  this.getMaintenanceRecord = () => {
    $http.post(`${SERVER_CONTEXT}/api/devicemaintainrecord/selectMaintainRecordList`,
        this.paginationData
          )
        .then((resp) => {
          this.maintenance = resp.data.data;
        });
  }

  // 获取检测情况
  this.getInspection = () => {
    $http.get(`${SERVER_CONTEXT}/api/devicecheckrecord/countCheckRecord/`
          )
        .then((resp) => {
          this.inspectionList = resp.data;
          this.inspectionEcharts();
        });
  }
  // 获取检测情况列表
  this.getInspectionList = () => {
    $http.post(`${SERVER_CONTEXT}/api/devicecheckrecord/queryCheckRecordList`,
        this.paginationData
          )
        .then((resp) => {
          this.detection = resp.data.data;
        });
  }

  // 初始化页面
  this.initPage = () => {

    // 获取机械设备情况汇总
    this.getEquipTotal();

    // 获取维保情况的维保次数
    this.getMaintenance();

    // 获取维保情况的设备记录
    this.getMaintenanceRecord();

    // 获取检测情况
    this.getInspection(); 

    // 获取检测情况列表
    this.getInspectionList();

  }

  this.initPage();

}

export default equipMgtCtrl;